package com.xw.unification.interceptor;

import com.xw.unification.helper.ApiVersionHelper;
import org.springframework.core.PriorityOrdered;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * 如果开启了 api-version, 则需要添加该重置器
 */
@WebFilter
public class ApiVersionHelperResettingFilter implements Filter, PriorityOrdered {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        InnerApiVersionHelper.resetVersion();
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public int getOrder() {
        return PriorityOrdered.HIGHEST_PRECEDENCE;
    }

    private static class InnerApiVersionHelper extends ApiVersionHelper {
        public static void resetVersion() {
            ApiVersionHelper.setVersion("");
        }
    }
}
